package com.winai.launcher.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.text.TextUtils;

import com.aiyisheng.speaker.main.IDuiSpeechListener;
import com.winai.launcher.constant.Constant;

public class AiyishengAIDLService extends Service {
    public AiyishengAIDLService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        System.out.println("AiyishengAIDLService started");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        System.out.println("AiyishengAIDLService destroyed");
    }

    @Override
    public IBinder onBind(Intent intent) {
        IDuiSpeechListener.Stub stub = new IDuiSpeechListener.Stub() {
            @Override
            public void onStartSpeak(String content, String ttsId) throws RemoteException {
                ttsSpeak(content, ttsId, "start");
            }

            @Override
            public void onStopSpeak(String ttsId) throws RemoteException {
                ttsSpeak(null, ttsId, "stop");
            }

            @Override
            public void onStartRecognition(String content, String ttsId) throws RemoteException {
                ttsDialog(content, ttsId, "start");
            }

            @Override
            public void onStopRecognition(String ttsId) throws RemoteException {
                ttsDialog(null, ttsId, "stop");
            }

            @Override
            public void onListeningState(int state) throws RemoteException {

            }
        };
        return stub;
    }

    /**
     * @param content 播报内容
     * @param ttsId 播报的ttsId
     * @param type start，开始播报；stop，停止播报
     *
     * @desc 广播通知语音播报或停止播报
     */
    private void ttsSpeak(String content, String ttsId, String type) {
        Intent intent = new Intent();
        intent.setAction(Constant.action_aiyisheng_text_speech);
        if (!TextUtils.isEmpty(content)) {
            intent.putExtra("content", content);
        }
        if (!TextUtils.isEmpty(ttsId)) {
            intent.putExtra("ttsId", ttsId);
        }
        if (!TextUtils.isEmpty(type)) {
            intent.putExtra("type", type);
        }
        sendBroadcast(intent);
    }
    /**
     * @param content 播报内容
     * @param ttsId 播报的ttsId
     * @param type start，开始播报；stop，停止播报
     *
     * @desc 广播通知IP形象对话打开或关闭
     */
    private void ttsDialog(String content, String ttsId, String type) {
        Intent intent = new Intent();
        intent.setAction(Constant.action_aiyisheng_dialog);
        if (!TextUtils.isEmpty(content)) {
            intent.putExtra("content", content);
        }
        if (!TextUtils.isEmpty(ttsId)) {
            intent.putExtra("ttsId", ttsId);
        }
        if (!TextUtils.isEmpty(type)) {
            intent.putExtra("type", type);
        }
        sendBroadcast(intent);
    }
}